package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.CompanyUsersMapper;
import com.px.model.CompanyUsers;
import com.px.model.getData;
import com.px.unit.Response;

@Controller
public class CompanyUsersController {
	CompanyUsersMapper companyusers_mapper = new CompanyUsersMapper();

	/**
	 * 登录请求方法
	 * */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/validateCompanyUsersLogin", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response validateCompanyUsersLogin(@RequestBody CompanyUsers companyusers) {
		CompanyUsers currentCompanyUser = companyusers_mapper.validateCompanyUsersLogin(companyusers);
		// validateUserLogin
		getData mydata = new getData();
		mydata.setCurrentCompanyUser(currentCompanyUser);
		if (currentCompanyUser != null) {
			// 登录成功
			return new Response(200, "登录成功", mydata);
		} else {
			// 登录失败
			return new Response(403, "登录失败");
		}
	}

	/**
	 * 获取指定Id单位负责人信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllCompanyUsers", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllCompanyUsers(@RequestBody CompanyUsers companyuser) {
		if (companyuser != null) {
			// 判断查询的会员单位负责人是否存在
			int checkid = companyusers_mapper.checkCompanyUsersIdIsExist(companyuser);
			if (!(checkid > 0)) {
				return new Response(304, "该会员单位负责人不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<CompanyUsers> CompanyUsersList = companyusers_mapper.getCompanyUsersByCompanyUsersId(companyuser);
				mydata.setCompanyUsersList(CompanyUsersList);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}

	/**
	 * 添加会员单位负责人信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/insertCompanyUsers", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response addUserInfo(@RequestBody CompanyUsers companyuser) {

		if (companyuser != null) {

			// 判断添加的会员单位负责人账户不能重复
			int checkname = companyusers_mapper.checkCompanyUsersIdIsExist(companyuser);
			if (checkname > 0) {
				return new Response(304, "已存在同名帐号");
			} else {
				int counts = companyusers_mapper.insertCompanyUsers(companyuser);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 根据会员单位负责人名删除会员单位负责人信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteCompanyUsersBCompanyUsersId", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteCompanyUsersBCompanyUsersId(@RequestBody CompanyUsers companyuser) {

		if (companyuser != null) {

			// 判断删除的会员单位负责人是否存在
			int checkname = companyusers_mapper.checkCompanyUsersIdIsExist(companyuser);
			if (!(checkname > 0)) {
				return new Response(304, "该会员单位负责人不存在");
			} else {
				int counts = companyusers_mapper.deleteCompanyUsersBCompanyUsersId(companyuser);
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 根据会员单位负责id修改会员单位负责人信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateCompanyUsersByCompanyUsersId", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateCompanyUsersByCompanyUsersId(@RequestBody CompanyUsers companyuser) {

		if (companyuser != null) {

			// 判断修改的会员单位负责人是否存在
			int checkname = companyusers_mapper.checkCompanyUsersIdIsExist(companyuser);
			if (!(checkname > 0)) {
				return new Response(304, "该会员单位负责人不存在");
			} else {
				int counts = companyusers_mapper.updateCompanyUsersByCompanyUsersId(companyuser);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

}
